package com.hanbang.maige.view.huodong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hanbang.maige.R;
import com.hanbang.maige.code.base.view.activity.BaseMvpActivity;
import com.hanbang.maige.code.broadcast.BroadcastUtils;
import com.hanbang.maige.mode.javabean.huodong.HuoDongPublishData;
import com.hanbang.maige.pressenter.huodong.PublishHuoDongPresenter;
import com.hanbang.maige.utils.animator.AnimUtils;
import com.hanbang.maige.utils.bitmap.GlideUtils;
import com.hanbang.maige.utils.other.DateUtils;
import com.hanbang.maige.utils.other.EditHelper;
import com.hanbang.maige.utils.other.StringUtils;
import com.hanbang.maige.utils.other.Validators;
import com.hanbang.maige.view.huodong.IHuoDongBaseView;
import com.hanbang.maige.view.login.activity.BiaoQianActivity;
import com.hanbang.maige.widget.ScaleImageView;
import com.hanbang.maige.widget.button.FlatButton;
import com.hanbang.maige.widget.dialog.DialogDateTime;
import com.hubng.photo_hander.PhotoHander;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.hanbang.maige.R.id.start_tm;
import static com.hanbang.maige.widget.dialog.DialogDateTime.MAX_YEAR;

/**
 * Created by 方朝辉 on 2016/10/25.
 */

public class PublishHuoDongActivity extends BaseMvpActivity<IHuoDongBaseView.IPublishHuoDongView, PublishHuoDongPresenter>
        implements IHuoDongBaseView.IPublishHuoDongView {

    @BindView(R.id.textInputEditText)
    TextInputEditText textInputEditText;
    @BindView(R.id.nameTil)
    TextInputLayout nameTil;
    @BindView(R.id.renshuTil)
    TextInputLayout renshuTil;
    @BindView(R.id.addressTil)
    TextInputLayout addressTil;
    @BindView(R.id.jieshaoTil)
    TextInputLayout jieshaoTil;
    @BindView(R.id.haibaoIv)
    ScaleImageView haibaoIv;
    @BindView(R.id.haibaoLay)
    RelativeLayout haibaoLay;
    @BindView(start_tm)
    TextView startTm;
    @BindView(R.id.end_tm)
    TextView endTm;
    @BindView(R.id.biaoqian)
    TextView biaoqianTv;
    @BindView(R.id.submitFb)
    FlatButton submitFb;
    @BindView(R.id.tishiTv)
    TextView tishiTv;

    HuoDongPublishData huoDongPublishData = new HuoDongPublishData();


    private EditHelper editHelper = new EditHelper(this);

    @Override
    public PublishHuoDongPresenter initPressenter() {
        return new PublishHuoDongPresenter();
    }

    public static void startUI(Context context) {
        Intent intent = new Intent(context, PublishHuoDongActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_publish_huodong;
    }

    @Override
    public void initView() {
        toolbar.setTitle("活动");
        toolbar.setBack(this);

        editHelper.addEditHelperData(new EditHelper.EditHelperData(nameTil, Validators.getLengthSRegex(1, 25), "请输入活动名称"));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(startTm, Validators.getLenghMinRegex(5), "请输入选择开始时间"));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(endTm, Validators.getLenghMinRegex(5), "请输入选择结束时间"));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(renshuTil, Validators.getLengthSRegex(1, 4), "请填写活动人数"));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(addressTil, Validators.getLengthSRegex(1, 30), "请填写活动地址"));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(jieshaoTil, Validators.getLengthSRegex(1, 100), "请填写活动介绍"));

        renshuTil.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!StringUtils.isEmpty(renshuTil.getEditText().getText().toString())) {
                    int num = Integer.valueOf(renshuTil.getEditText().getText().toString());
                    if (num > 1000) {
                        s.clear();
                        s.append("1000");
                    }
                }
            }
        });
    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public void clearData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.haibaoLay, start_tm, R.id.end_tm, R.id.biaoqian})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.haibaoLay:
                PhotoHander.create()
                        .single()
                        .crop(16, 9)
                        .showCamera(true)
                        .compress(true)
                        .start(PublishHuoDongActivity.this, REQUEST_CODE + 1);
                break;
            case start_tm:
                seletcTime(true);

                break;
            case R.id.end_tm:
                seletcTime(false);
                break;
            case R.id.biaoqian:
                BiaoQianActivity.startUI(this);
                break;
        }
    }

    private void seletcTime(final boolean isStart) {
        DialogDateTime dialogDateTime = new DialogDateTime(this, DialogDateTime.MODE.DATE_Y_M_D_H_M);
        dialogDateTime.setTitleMain(isStart ? "开始时间" : "结束时间");
        Calendar min;
        Calendar max;
        if (isStart) {
            min = Calendar.getInstance();

            if (huoDongPublishData.endDate == null) {
                max = null;
            } else {
                max = (Calendar) huoDongPublishData.endDate.clone();
            }
            dialogDateTime.setInitTime(huoDongPublishData.startDate);
        } else {
            if (huoDongPublishData.startDate == null) {
                min = Calendar.getInstance();
                max = null;
            } else {
                min = (Calendar) huoDongPublishData.startDate.clone();
                max = (Calendar) huoDongPublishData.startDate.clone();
                max.add(Calendar.YEAR, MAX_YEAR);
            }
            dialogDateTime.setInitTime(huoDongPublishData.endDate);
        }
        dialogDateTime.setMinCalendar(min);
        dialogDateTime.setMaxCalendar(max);

        dialogDateTime.setOnClickCallback(new DialogDateTime.OnClickCallback() {
            @Override
            public void onComplete(DialogDateTime dialog, Calendar content) {

                if (isStart) {
                    huoDongPublishData.startDate = content;
                    startTm.setText("开始 " + DateUtils.timeToString(huoDongPublishData.startDate, "yyyy-MM-dd HH:mm"));
                } else {
                    huoDongPublishData.endDate = content;
                    endTm.setText("结束 " + DateUtils.timeToString(huoDongPublishData.endDate, "yyyy-MM-dd HH:mm"));
                }
            }
        });
        dialogDateTime.show();
    }

    ///
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE + 1 && resultCode == RESULT_OK) {
            List<String> mSelectPath = data.getStringArrayListExtra(PhotoHander.EXTRA_RESULT);
            if (mSelectPath != null && mSelectPath.size() != 0) {
                huoDongPublishData.haibaoPath = mSelectPath.get(0);
                GlideUtils.show(haibaoIv, huoDongPublishData.haibaoPath);
                tishiTv.setVisibility(View.GONE);
            }
        }
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            huoDongPublishData.biaoqianData = data.getParcelableExtra("data");
            biaoqianTv.setText(getString(R.string.huodongBq) + ":" + huoDongPublishData.biaoqianData.getHyValueName());
        }

    }

    @OnClick(R.id.submitFb)
    public void onClick() {
        if (StringUtils.isEmpty(huoDongPublishData.haibaoPath)) {
            AnimUtils.shakeLeft(haibaoIv, 0.85f, 6f);
            showWarningSnackbar("请为活动选择一张海报");
            return;
        }
        if (editHelper.check()) {

            if (huoDongPublishData.biaoqianData == null) {
                AnimUtils.shakeLeft(biaoqianTv, 0.85f, 6f);
                showWarningSnackbar("请为活动选择标签");
                return;
            }
            huoDongPublishData.name = editHelper.getText(R.id.nameTil);
            huoDongPublishData.huodongRenshu = editHelper.getText(R.id.renshuTil);
            huoDongPublishData.huodongAddress = editHelper.getText(R.id.addressTil);
            huoDongPublishData.huodongJieShao = editHelper.getText(R.id.jieshaoTil);

            presenter.postHuodong(huoDongPublishData);


        }

    }

    @Override
    public void complete() {
        Intent intent = new Intent(BroadcastUtils.HUODONG_PUBLISH);
        sendBroadcast(intent);
        finish();
    }
}
